/**
 *******************************************************************************
 * @file cli_task.c
 * @author .ISS_AUTO (hudandan@issauto.com)
 * @date 2024-05-15  1. Base Version；
 *       2025-03-19  1. Remove redundant code
 *
 * @brief  This file for the cli task specification.
 *
 * @copyright Copyright (c) 2024 ISSAUTO TECH Co., Ltd. All rights reserved.
 *
 *******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "cli_task.h"
#include "commun_queue.h"

/* Define --------------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static unsigned char RING_RX_BUF_CLI[COMM_CLI_RX_BUF_SIZE * 2] = {0};

/** @addtogroup CLI
  * @{
  */
static hcli_adapter_t h_cli_adapt; /*!< hcli控适配器 */
static hcli_obj_t h_cli_controller; /*!< hcli控制器 */
static unsigned char h_cli_rx_buffer[COMM_CLI_RX_BUF_SIZE]; /*!< 数据接收缓冲区 */

/**
  * @}
  */


/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* PRQA S 3384 ++ */

/*******************************************************************************
 *                            Serial Interface
********************************************************************************/

/**
 * @brief	    读取串口接收缓冲区的数据
 * @param[in]   buf       -  数据缓存
 * @param[in]   len       -  数据长度
 * @return 	    (实际读取长度)如果接收缓冲区的有效数据大于len则返回len否则返回缓冲
 *              区有效数据的长度
 */
static unsigned int cli_recv_read(void *buf, unsigned int len)
{
    return comm_receive_bytes_pop(COMM_CHANNEL_CLI, (unsigned char *)buf, len);
}

/**
 * @brief 串口发送函数
 * 			根据开发者MCU自行填充串口发送函数
 * @param buf
 * @param len
 */
static unsigned int cli_send_data(const void *buf, unsigned int len)
{
    return USARTx_SendData_Blocking(UART_CHANNEL_CLI, (const unsigned char *)buf, len);
}

/**
 * @brief	    自定义协议解析
 * @param[in]   buf -  数据缓存
 * @param[in]   len -  数据长度
 */
static void cli_protocol_process(void *obj, void *buf, unsigned int len)
{
    hcli_recv_process((hcli_obj_t *)obj, (char *)buf, len);
}

/*******************************************************************************
 *                                 CLI
********************************************************************************/

/**
 * @brief return cli object
 *
 * @return hcli_obj_t
 */
hcli_obj_t *cli_obj_get(void)
{
    return &h_cli_controller;
}

/**
 * @brief 添加命令行
 *
 * @param cmd
 * @param n
 * @return int
 */
int cli_cmds_add(hcli_cmd_t *cmd, unsigned int n)
{
    return hcli_cmds_add(&h_cli_controller, cmd, n);
}

/**
 * @brief 删除命令行
 *
 * @param cmd
 * @param n
 * @return int
 */
int cli_cmds_del(hcli_cmd_t *cmd, unsigned int n)
{
    return hcli_cmds_del(&h_cli_controller, cmd, n);
}

/**
 * @brief Software/Hardware deinitialization.
 *
 * @param[in] void
 * @return void
 */
void CLI_DeInit(void)
{
    /*!< Hardware deinitialization -------------------------------------*/
    #if (UART_CHANNEL_CLI != UART_CHANNEL_PRINTF)
    NVIC_DisableIRQ(UART_IRQn_CLI); //!< Uartx interrupt disabled
    #endif

    /*!< Software deinitialization -------------------------------------*/
    Comm_RingQueue_Clear(COMM_CHANNEL_CLI);

}

/**
 * @brief Software/Hardware Initialization.
 *
 * @param[in] void
 * @return void
 */
void CLI_Init(void)
{
    /*!< Hardware initialization ---------------------------------------*/

    #if (UART_CHANNEL_CLI != UART_CHANNEL_PRINTF)
    UartX_Configuration(UART_CHANNEL_CLI, UART_BAUDRATE_CLI);
    #endif
    UartX_ConfigurationInt(UART_CHANNEL_CLI, ENABLE, 1);
    Comm_RingQueue_Buff_Init(COMM_CHANNEL_CLI,
                             RING_RX_BUF_CLI, sizeof(RING_RX_BUF_CLI),
                             NULL, 0U);


    /*!< Software initialization ---------------------------------------*/
    memset((void *)&h_cli_adapt, 0, sizeof(h_cli_adapt));

    h_cli_adapt.read_passthrough = NULL; //!< 发送透传接口
    h_cli_adapt.write         = cli_send_data;
    h_cli_adapt.read          = cli_recv_read;
    h_cli_adapt.user_process  = cli_protocol_process;
    h_cli_adapt.echo          = 0; //!< 失能回显
    h_cli_adapt.cli_cmds_num  = 0;
    h_cli_adapt.cli_buf       = h_cli_rx_buffer;
    h_cli_adapt.cli_bufsize   = sizeof(h_cli_rx_buffer);
    h_cli_adapt.cli_cnt       = 0;
    h_cli_adapt.cli_timer     = 0;

    /*!< 初始化hcli控制器 */
    hcli_init(&h_cli_controller, &h_cli_adapt);

}

/**
 * @brief Handle function for Application Driver layer
 *
 * @param[in] void
 * @return DRV Status
 */
void CLI_Handle(void *arg)
{
    (void)arg;

    /*!< 命令行任务轮询*/
    hcli_process(&h_cli_controller, 2);
}

/* PRQA S 3384 -- */
/* END OF FILE ---------------------------------------------------------------*/
